<?php

namespace app\api\model;

use app\common\model\BaseModel;

class MerchantCar extends BaseModel
{
    // 表名
    protected $name = 'merchant_car';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 追加属性
    protected $append = [
    ];


    //车辆信息
    public static function carinfo($uid){
        //门店车辆
        $allnum = self::where(['is_delete' => 1, 'merchant_id' => $uid])->count();
        //已租车辆
        $usenum = self::where(['is_delete' => 1, 'merchant_id' => $uid, 'car_status' => 2])->count();
        //可租车辆
        $surplus = self::where(['is_delete' => 1, 'merchant_id' => $uid, 'car_status' => 1, 'examine_status' => 2, 'status' => 1])->count();
        //审核中车辆数
        $review = self::where(['is_delete' => 1, 'merchant_id' => $uid, 'examine_status' => 1])->count();
        //审核未通过
        $fail = self::where(['is_delete' => 1, 'merchant_id' => $uid, 'examine_status' => 3])->count();
        $total = [
            'allnum' => $allnum,
            'usenum' => $usenum,
            'surplus' => $surplus,
            'review' => $review,
            'fail' => $fail,
        ];
        return $total;
    }


    /**
     * 获取列表
     * @param array $where
     * @param string $field
     * @param int $page
     * @param int $limit
     * @return Driver[]|array|\think\Collection
     */
    public static function getList($where = [], $field = '*', $order = 'createtime desc', $page = 1, $limit = 10, $with = 'car'){
        if($page){
            return self::field($field)->withJoin($with)->where($where)->order($order)->page($page, $limit)->select();
        }else{
            return self::field($field)->withJoin($with)->where($where)->field($field)->order($order)->select();
        }
    }

    /**
     * 获取单条
     * @param array $where
     * @param string $field
     * @return Driver|array|mixed|\think\Model|null
     */
    public static function getFind($where = [], $field = '*'){
        return self::where($where)->field($field)->lock(true)->find();
    }

    /**
     * 获取单列
     * @param array $where
     * @param string $column
     * @return array
     */
    public static function getColumn($where = [], $column = 'merchant_id'){
        return self::where($where)->column($column);
    }

    public function car(){
        return $this->hasOne('car','id','car_id');
    }

    public function cars(){
        return $this->hasOne('car','id','car_id')->bind(['car_name','car_image','car_site','car_gate','car_gear', 'drive', 'displacement', 'car_details']);
    }

    public function carss(){
        return $this->hasOne('car','id','car_id')->bind(['car_name','car_image','car_site','car_gate','car_gear', 'drives', 'displacements', 'car_details', 'car_banner' =>'banner']);
    }

    public function merchant(){
        return $this->hasOne('merchant','id','merchant_id')->joinType('full');
    }

    public function merchants(){
        return $this->hasOne('merchant','id','merchant_id')->bind(['shop_logo','shop_name','province','city','area', 'street', 'longitude', 'latitude', 'service']);
    }

    public static function fetchCarGear($car_gear = null){
        $data = [
            1 => '手动挡',
            2 => '自动挡'
        ];
        if(isset($data[$car_gear])){
            return $data[$car_gear];
        }else{
            return $data;
        }
    }
}
